package com.yiwei.hdb.activity;

import android.annotation.TargetApi;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.api.Api;
import com.yiwei.hdb.api.bean.CodeBean;
import com.yiwei.hdb.api.bean.ResetNewPassBean;
import com.yiwei.hdb.util.MD5;
import com.yiwei.hdb.util.Network;
import com.yiwei.hdb.util.RxLifeUtil;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.UI;
import com.yiwei.hdb.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 18-6-8.
 */

public class V3ForgetPassActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_phone)
    EditText editPhone;
    @BindView(R.id.btn_close)
    ImageView btnClose;
    @BindView(R.id.edit_code)
    EditText editCode;
    @BindView(R.id.btn_send_code)
    TextView btnSendCode;
    @BindView(R.id.edit_pass)
    EditText editPass;
    @BindView(R.id.btn_see_pass)
    ImageView btnSeePass;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private Unbinder unbinder;
    private boolean isChecked = false;

    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_v3_forget_pass);
        unbinder = ButterKnife.bind(this);
        initBar();
        init();
    }

    private void init() {
        txtTitle.setText("忘记密码");
        setIsNeedCheckLogout(false);
    }

    public void getResetNewPass(String mobile, String password, String verify_code) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        ResetNewPassBean bean = new ResetNewPassBean();
        bean.mobile = mobile;
        bean.password = MD5.getMD5str(password);
        bean.verify_code = verify_code;
        Api.api_service.getResetNewPass(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "修改成功").show();
                    finish();
                });
    }

    public void getCode(String mobile) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        CodeBean bean = new CodeBean();
        bean.mobile = mobile;
        bean.type = "sms";
        Api.api_service.getV3SendCode(bean).compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    if (result.getStatus() == 1) {
                        ToastUtil.makeText(this, "已发送验证码").show();
                        timer.start();
                    } else {
                        ToastUtil.makeText(this, result.getMsg()).show();
                    }
                });
    }

    private CountDownTimer timer = new CountDownTimer(60000L, 1000L) {
        public void onFinish() {
            resetBtn();
        }

        @TargetApi(16)
        public void onTick(long paramAnonymousLong) {
            btnSendCode.setEnabled(false);
            btnSendCode.setText(paramAnonymousLong / 1000L + "s");
        }
    };

    @OnTextChanged({R.id.edit_phone, R.id.edit_code, R.id.edit_pass})
    void afterTextChanged(Editable s) {
        if (UI.toString(editPhone).length() == 11 && !TextUtils.isEmpty(UI.toString(editCode)) && !TextUtils.isEmpty(UI.toString(editPass))) {
            btnSubmit.setSelected(true);
        } else {
            btnSubmit.setSelected(false);
        }
    }


    @TargetApi(16)
    private void resetBtn() {
        if (this.timer != null)
            this.timer.cancel();

        btnSendCode.setText("发送");
        btnSendCode.setEnabled(true);
    }

    private boolean checkPhone() {
        if (TextUtils.isEmpty(UI.toString(editPhone))) {
            ToastUtil.makeText(this, getResources().getString(R.string.phone_is_empty), 0);
            return false;
        }
        if (UI.toString(editPhone).length() < 11) {
            ToastUtil.makeText(this, getResources().getString(R.string.phone_is_error), 0);
            return false;
        }
        return true;
    }

    private boolean checkCode() {
        if (TextUtils.isEmpty(UI.toString(editCode))) {
            ToastUtil.makeText(this, getResources().getString(R.string.code_is_empty), 0);
            return false;
        }
        return true;
    }

    private boolean checkNewPwd() {
        if (TextUtils.isEmpty(UI.toString(editPass))) {
            ToastUtil.makeText(this, getResources().getString(R.string.password_is_empty), 0);
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_close, R.id.btn_send_code, R.id.btn_see_pass, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_close:
                editPhone.setText("");
                break;
            case R.id.btn_send_code:
                if (checkPhone()) {
                    getCode(UI.toString(editPhone));
                }
                break;
            case R.id.btn_see_pass:
                isChecked = !isChecked;
                if (isChecked) {
                    //如果选中，显示密码
                    btnSeePass.setSelected(true);
                    editPass.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    btnSeePass.setSelected(false);
                    editPass.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.btn_submit:
                if (checkPhone() && checkCode() && checkNewPwd()) {
                    getResetNewPass(UI.toString(editPhone), UI.toString(editPass), UI.toString(editCode));
                }
                break;
        }
    }
}
